//
//  SEGVisitorAccessCodeViewController.m
//  ExcellentHome
//
//  Created by 乔杨 on 2020/6/4.
//
//访客通行证界面

#import "SEGVisitorAccessCodeViewController.h"

#import "SEGVisitorAccessCodeInfoCell.h"
#import "SEGVisitorAccessCodeDoorCell.h"

#import <MessageUI/MessageUI.h>

#import "SEGVisitorAccessCodeViewModel.h"

@interface SEGVisitorAccessCodeViewController ()
<UITableViewDelegate,
UITableViewDataSource,
SEGClickDoorListDelegate,
MFMessageComposeViewControllerDelegate
>

/// 信息列表视图
@property (nonatomic, strong)UITableView *tableView;
/// 生成通行证按钮
@property (nonatomic, strong)UIButton *shareButton;

/// viewModel
@property (nonatomic, strong) SEGVisitorAccessCodeViewModel *viewModel;

@end

@implementation SEGVisitorAccessCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"访客通行证";
    [self showCustomNavigationBackButton];
    self.view.backgroundColor = kColor_bg;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.shareButton];
    [self seg_shareBind];
}

#pragma mark - Event
- (void)seg_shareBind {
    @weakify(self);
    [[self.shareButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self);
        NSDictionary *mParam = [self.viewModel seg_getShareInfo]; // 获取分享信息
        UIViewController *shareVC = [[SEGUhomeMediator sharedInstance] seg_initShareViewController:mParam actionType:^(NSInteger actionType) {
            if (actionType == SEGShareVCActionType_Share_Message) {
                Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
                if (messageClass != nil) {
                    if ([messageClass canSendText]) {
                        MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
                        picker.messageComposeDelegate = self;
                        NSString *smsBody = [NSString stringWithFormat:@"您的访客通行证：%@，请在进入园区时，出示此通行证给门岗。【%@】", self.viewModel.shareUrl, kAppName];
                        picker.body = smsBody;
                        picker.title = @"访客通行证";
                        picker.modalPresentationStyle = UIModalPresentationFullScreen;
                        [[[UIApplication sharedApplication] keyWindow].rootViewController presentViewController:picker animated:YES completion:^{
                        }];
                    } else {
                        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"设备不支持短信功能" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                        [alert show];
                    }
                }
            }
        } channel:^(NSString *channel) {
            
        }];
        [self presentViewController:shareVC animated:NO completion:nil];
    }];
}

#pragma mark - MFMessageComposeViewControllerDelegate
- (void)messageComposeViewController:(MFMessageComposeViewController*)controller didFinishWithResult:(MessageComposeResult)result {
    [controller dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (self.viewModel.codeModel.visitorDoors.count > 0) return 2;
    return 1;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        SEGVisitorAccessCodeInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:kVisitorAccessCodeInfoCellIdentifier forIndexPath:indexPath];
        [cell updateInfoCellWithModel:self.viewModel.codeModel];
        return cell;
    } else {
        SEGVisitorAccessCodeDoorCell *cell = [tableView dequeueReusableCellWithIdentifier:kVisitorAccessCodeDoorCellIdentifier forIndexPath:indexPath];
        if (!cell.delegate) {
            cell.delegate = self;
            [cell updateDoorInfoWithModel:self.viewModel.codeModel];
        }
        return cell;
    }
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) return 443.0;
    return self.viewModel.doorInfoCellHeight;
}

#pragma mark - SEGClickDoorListDelegate
- (void)updateDoorInfoCellWithHeight:(CGFloat)height {
    self.viewModel.doorInfoCellHeight = height;
    [self.tableView reloadData];
}

#pragma mark - Getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-kNavigationBarAndStatusBarHeight-44-LYSafeAreaBottom()) style:UITableViewStylePlain];
        _tableView.backgroundColor = kColor_bg;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        [_tableView registerClass:[SEGVisitorAccessCodeInfoCell class] forCellReuseIdentifier:kVisitorAccessCodeInfoCellIdentifier];
        [_tableView registerClass:[SEGVisitorAccessCodeDoorCell class] forCellReuseIdentifier:kVisitorAccessCodeDoorCellIdentifier];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (UIButton *)shareButton {
    if (!_shareButton) {
        _shareButton = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.bounds)-kNavigationBarAndStatusBarHeight-44-LYSafeAreaBottom(), LYScreenWidth(), 44+LYSafeAreaBottom())];
        _shareButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, LYSafeAreaBottom(), 0);//适配iPhoneX 按钮文字向上偏移安全区域底部的距离
        [_shareButton setBackgroundImage:[UIImage imageWithColor:kColor_T] forState:UIControlStateNormal];
        [_shareButton setBackgroundImage:[UIImage imageWithColor:kColor_T_Press] forState:UIControlStateHighlighted];
        [_shareButton setTitleColor:kColor_W forState:UIControlStateNormal];
        _shareButton.titleLabel.font = kFont_16;
        [_shareButton setTitle:@"分享给访客" forState:UIControlStateNormal];
    }
    return _shareButton;
}
- (SEGVisitorAccessCodeViewModel *)viewModel {
    if (!_viewModel) _viewModel = [[SEGVisitorAccessCodeViewModel alloc] init];
    return _viewModel;
}

@end
